/**
 * Copyright 2007 Luke Pillow
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orsomethinglikethat.hinge.asgen;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.orsomethinglikethat.hinge.apt.BeanCompilerUtils;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.FieldDeclaration;

/**
 * @author lpillow
 */
public class StringBuilderAS3ClassGenerator implements AS3ClassGenerator
{
    FileOutputStream fos;
    StringBuilder as;

    private static final String REMOTE_PATTERN = "[RemoteClass(alias=\"%1$s\")]";

    public StringBuilderAS3ClassGenerator()
    {
        as = new StringBuilder();
    }

    private void writeProperties( ClassDeclaration clazz )
    {
        for ( FieldDeclaration field : BeanCompilerUtils.getBeanFields( clazz ) )
        {
            as.append( "\t\tpublic var " ).append( field.getSimpleName() ).append( ":" ).append( ASGenTypeConverter.convertJavaType( field.getType().toString() ) ).append( ";\n" );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.orsomethinglikethat.hinge.asgen.AS3ClassGenerator#writeAS3Class(com.sun.mirror.declaration.ClassDeclaration)
     */
    public void generateClass( ClassDeclaration clazz, String userClassesHomePath )
    {
        as.append( "package " ).append( clazz.getPackage().toString() ).append( "\n" );
        as.append( "{\n" );
        as.append( "\t[Managed]" ).append( "\n" );
        as.append( "\t" ).append( String.format( REMOTE_PATTERN, clazz.getQualifiedName() ) ).append( "\n" );
        as.append( "\tpublic class " ).append( clazz.getSimpleName() ).append( "\n" );
        as.append( "\t{\n" );

        this.writeProperties( clazz );
        as.append( "\n" );
        as.append( "\t\tpublic function " ).append( clazz.getSimpleName() ).append( "() {}\n" );

        as.append( "\t}\n" );
        as.append( "}" );

        try
        {
            String path = clazz.getQualifiedName().substring( 0, clazz.getQualifiedName().lastIndexOf( "." ) ).replace( ".", File.separator );
            String home = userClassesHomePath.charAt( userClassesHomePath.length() - 1 ) == File.separatorChar ? userClassesHomePath : userClassesHomePath + File.separator;
            File outputFile = new File( home + path );
            outputFile.mkdirs();
            fos = new FileOutputStream( outputFile.getAbsolutePath() + File.separator + clazz.getSimpleName() + ".as" );
            fos.write( as.toString().getBytes() );
        }
        catch ( Exception e )
        {

        }
        finally
        {
            try
            {
                fos.close();
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
        }
    }
}
